package com.example.gomoku;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.gomoku.CircularImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private CircularImageView avatarImageView;
    private ImageButton toggleButton;
    private boolean isPlaying = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        Intent musicIntent = new Intent(this, BackgroundMusicService.class);
        startService(musicIntent);
        initView();
        getUserAvatar();
        toggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent toggleIntent = new Intent(MainActivity.this, BackgroundMusicService.class);
                toggleIntent.setAction("ACTION_TOGGLE");
                startService(toggleIntent);
                isPlaying = !isPlaying;
                updateButtonImage();
            }
        });
    }
    protected void onDestroy() {
        super.onDestroy();
        // 停止背景音乐服务
        Intent musicIntent = new Intent(this, BackgroundMusicService.class);
        musicIntent.setAction("ACTION_STOP");
        stopService(musicIntent);
    }

    private void getUserAvatar() {
        User currentUser = getCurrentUser(); // 获取当前用户信息的方法
        if (currentUser != null) {
            if (currentUser.getUavatar() != null) {
                Bitmap bitmap = BitmapFactory.decodeByteArray(currentUser.getUavatar(), 0, currentUser.getUavatar().length);
                avatarImageView.setImageBitmap(bitmap);
            }
        } else {
            Log.d("MainActivity", "No current user found");
        }
    }

    private void initView() {
        findViewById( R.id.start ).setOnClickListener( this );
        findViewById( R.id.begin ).setOnClickListener( this );
        findViewById( R.id.personcenter ).setOnClickListener( this );
        toggleButton=findViewById( R.id.toggleButton );
        // 初始化视图
        avatarImageView = findViewById(R.id.avatarImageView);

    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.start){
            Intent i = new Intent(this, GomokuActivity.class );
            startActivity( i );

        } else if (v.getId()==R.id.personcenter) {
            Intent i = new Intent(this,PersonCenterActivity.class);
            startActivity( i );

        }
        else {
            if(v.getId()==R.id.begin){
                Intent i = new Intent(this, MainActivity2.class );
                startActivity( i );
        }
    }
}
private User getCurrentUser() {
        SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        String currentUid = sharedPreferences.getString("uid", null);
        Log.d("MainActivity", "Current UID: " + currentUid);
        if (currentUid != null) {
            DBHelper dbHelper=new DBHelper( this );
            return dbHelper.getUserByUID(currentUid);
        }
        return null;
    }

    private void updateButtonImage() {
        if (isPlaying) {
            toggleButton.setImageResource(R.drawable.baseline_music_off_24);
        } else {
            toggleButton.setImageResource(R.drawable.baseline_music_note_24);
        }
    }
}